{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fca0ccdf-d820-4b6d-881b-d1aa7230b708",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.9/site-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.16.5 and <1.23.0 is required for this version of SciPy (detected version 1.24.1\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+------+--------------------+--------------------+--------------------+\n",
      "| meta | R                  | G                  | B                  |\n",
      "+------+--------------------+--------------------+--------------------+\n",
      "| max  | 255                | 255                | 255                |\n",
      "+------+--------------------+--------------------+--------------------+\n",
      "| min  | 0                  | 0                  | 0                  |\n",
      "+------+--------------------+--------------------+--------------------+\n",
      "| mean | 181.42300533333332 | 169.47594566666666 | 151.87742033333333 |\n",
      "+------+--------------------+--------------------+--------------------+\n",
      "| std  | 61.4472374629048   | 69.72452856810297  | 83.22114303811752  |\n",
      "+------+--------------------+--------------------+--------------------+\n",
      "| 25%  | 144.0              | 121.0              | 79.0               |\n",
      "+------+--------------------+--------------------+--------------------+\n",
      "| 50%  | 198.0              | 192.0              | 189.0              |\n",
      "+------+--------------------+--------------------+--------------------+\n",
      "| 75%  | 228.0              | 224.0              | 222.0              |\n",
      "+------+--------------------+--------------------+--------------------+\n"
     ]
    }
   ],
   "source": [
    "#  引入合适的包\n",
    "from tabulate import tabulate\n",
    "import numpy as np\n",
    "from skimage import io\n",
    "\n",
    "#  读入RGB图:  dog.jpg\n",
    "img = io.imread(\"dog.jpg\")\n",
    "\n",
    "#  把图像变成2维结构\n",
    "#  该结构只有3列，分别对应R,  G,  B三个通道\n",
    "img = img.reshape(-1, 3)\n",
    "\n",
    "rgb_max = np.max(img, axis=0)  # 沿着0轴计算RGB最大值列表\n",
    "\n",
    "rgb_min = np.min(img, axis=0)  # 沿着0轴计算RGB最小值列表\n",
    "\n",
    "rgb_mean = np.mean(img, axis=0)  # 沿着0轴计算RGB平均值列表\n",
    "\n",
    "rgb_std = np.std(img, axis=0)  # 沿着0轴计算RGB标准正差列表\n",
    "\n",
    "rgb_25p = np.percentile(img, 25, axis=0)  # 沿着0轴计算RGB值排序后  25%  值列表\n",
    "\n",
    "rgb_50p = np.percentile(img, 50, axis=0)  # 沿着0轴计算RGB值排序后  50%  值(中位数)列表\n",
    "\n",
    "rgb_75p = np.percentile(img, 75, axis=0)  # 沿着0轴计算RGB值排序后  75%  值列表\n",
    "\n",
    "result = [\n",
    "    ['meta', 'R', 'G', 'B'],\n",
    "    ['max'] + rgb_max.tolist(),\n",
    "    ['min'] + rgb_min.tolist(),\n",
    "    ['mean'] + rgb_mean.tolist(),\n",
    "    ['std'] + rgb_std.tolist(),\n",
    "    ['25%'] + rgb_25p.tolist(),\n",
    "    ['50%'] + rgb_50p.tolist(),\n",
    "    ['75%'] + rgb_75p.tolist()\n",
    "]\n",
    "\n",
    "print(tabulate(result, tablefmt='grid'))  # 打印结果\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
